
package com.Spring.SpringSpms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

import com.Spring.SpringSpms.model.Employee;
import com.Spring.SpringSpms.model.Story;
import com.Spring.SpringSpms.model.Task;

public class TaskDAOImpl implements TaskDAO {

private JdbcTemplate jdbcTemplate;
public TaskDAOImpl(DataSource dataSource) {
		 jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void saveOrUpdate(Task task) {
		System.out.print("the taskid is:"+task.getTask_id());
		if( (task.getTask_id()) >0)
		{
			System.out.print("in update");
			String sql="UPDATE Task SET task_title=?,task_description=?,task_owner=?,complexity_id =?,status_id =?,actual_effort=? AND remaining_effort=? WHERE task_id=?";
			jdbcTemplate.update(sql,task.getTask_title(),task.getTask_description(),task.getTask_owner(),task.getComplexity_id(),task.getStatus_id(),task.getActual_effort(),task.getRemaining_effort(),task.getTask_id());
			
		} else {
			System.out.print("in insert");
			// insert
			String sql = "INSERT INTO Task (story_id,task_title,task_description,task_owner,complexity_id,status_id,actual_effort,remaining_effort)" + " VALUES (?,?,?,?,?,?,?,?)";
						jdbcTemplate.update(sql,task.getStory_id(),task.getTask_title(),task.getTask_description(),task.getTask_owner(),task.getComplexity_id(),task.getStatus_id(),task.getActual_effort(),task.getRemaining_effort());
		}
	}

	@Override
	public void delete(int task_id) {
		String sql = "delete from Task where task_id="+ task_id;
		jdbcTemplate.update(sql);
		
	}

	@Override
	public Task get(int task_id) {
		String sql = "SELECT * FROM Task WHERE task_id=" + task_id;
		return jdbcTemplate.query(sql, new ResultSetExtractor<Task>() {

			@Override
			public Task extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				if (rs.next()) {
					Task t = new Task();
					t.setTask_id(rs.getInt("task_id"));
					t.setStory_id(rs.getInt("story_id"));
					t.setTask_title(rs.getString("task_title"));
					t.setTask_owner(rs.getString("task_owner"));
					t.setTask_description(rs.getString("task_description"));
					t.setComplexity_id(rs.getInt("complexity_id"));
					t.setActual_effort(rs.getInt("actual_effort"));
					t.setRemaining_effort(rs.getInt("remaining_effort"));
					
					return t;
				}
				
				return null;
			}
			
		});

}

	@Override
	public List<Task> list(int story_id) {
		String sql = "SELECT * FROM Task where story_id="+story_id;
		List<Task> listTask = jdbcTemplate.query(sql, new RowMapper<Task>() {

			@Override
			public Task mapRow(ResultSet rs, int rowNum) throws SQLException {
				Task t = new Task();
	
				t.setTask_id(rs.getInt("task_id"));
				t.setStory_id(rs.getInt("story_id"));
				t.setTask_title(rs.getString("task_title"));
				t.setTask_description(rs.getString("task_description"));
				t.setTask_owner(rs.getString("task_owner"));
				t.setComplexity_id(rs.getInt("complexity_id"));
				t.setStatus_id(rs.getInt("status_id"));
				t.setRemaining_effort(rs.getInt("remaining_effort"));
				return t;
			}
			
		});
		
		return listTask;
	}
	/*
	 * getStatus takes task id as input and finds out the specific status_name associated with that particular task
	 */
	@Override
	public String getStatus(int tid) {
		String sql="select status_name from Status where status_id in(select status_id from Task where task_id=?)";
		String name=jdbcTemplate.queryForObject(sql, String.class,tid);
	return name;
}
	
	@Override
	public List<String> listStatus(int sid) {
		
//		String sql="select status_name from status where status_id in(select status_id from task where story_id=?)";
		String sql="select Status.status_name from Status,Task where Status.status_id=Task.status_id and Task.story_id=? order by task_id";
		List<String> statuslist=(List<String>) jdbcTemplate.queryForList(sql,String.class,sid);		
		return statuslist;
	}
	
	@Override
	public String getComplexity(int tid)
	{
		String sql="select Complexity.complexity_name from Complexity,Task where Complexity.complexity_id=Task.complexity_id and Task.task_id=?";
		String name=jdbcTemplate.queryForObject(sql, String.class,tid);
		return name;
	}

	
	
}